package com.jcfk.eam.service.csm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.csm.CsmProductRecordDao;
import com.jcfk.eam.domain.dto.csm.CsmProductRecordDTO;
import com.jcfk.eam.domain.po.csm.CsmProductRecord;
import com.jcfk.eam.domain.query.csm.CsmProductRecordQuery;
import com.jcfk.eam.domain.uploadExcel.csm.CsmProductRecordUploadExcel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 耗材生产记录
 *
 * @author yrl
 * @email
 * @date 2024-11-05
 */
@Service
public class CsmProductRecordService extends BaseService<CsmProductRecordDao, CsmProductRecord> {

    /**
     * CsmProductRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<CsmProductRecordDTO> queryByPage(DataPage<CsmProductRecordDTO> page, CsmProductRecordQuery query) {
        // 分页查询
        query.setConsumeCode(StrUtil.isNotEmpty(query.getConsumeCode())? StrUtil.format("%{}%",query.getConsumeCode()):null);
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断CsmProductRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(CsmProductRecordDTO dto) {
        QueryWrapper<CsmProductRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入耗材生产记录
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(CsmProductRecordDTO dto) {

        // 保存
        CsmProductRecord item = new CsmProductRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新耗材生产记录
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CsmProductRecordDTO dto) {

        // 保存
        CsmProductRecord item = new CsmProductRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除耗材生产记录(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(CsmProductRecordDTO dto) {

        CsmProductRecord item = new CsmProductRecord();

        this.setDefaultValue(item);
        item.setDeleted("1");

        QueryWrapper<CsmProductRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(CsmProductRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
                item.setDeleted("0");
                                                                            item.setCreatedUserName(SecurityUtils.getUserName());
                                                                                                                                                                    }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

                                    item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取耗材生产记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public CsmProductRecordDTO get(String id) {
        // 查询数据
        CsmProductRecord item = this.baseMapper.selectById(id);
            CsmProductRecordDTO dto = new CsmProductRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 插入耗材生产记录
     *
     * @param list 批量插入耗材生产记录
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<CsmProductRecordUploadExcel> list) {

        List<CsmProductRecord> savelist = new ArrayList<>();
        list.forEach(itemOne ->{
                // 保存
            CsmProductRecord item = new CsmProductRecord();
            BeanUtils.copyProperties(itemOne, item);
            this.setDefaultValue(item);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBigBatch(savelist);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<CsmProductRecordDTO>
     */
    public List<CsmProductRecordDTO> getList(CsmProductRecordQuery query) {
        // 查询条件
        QueryWrapper<CsmProductRecord> qw = new QueryWrapper<>();
                                                                                                                                                                                                                                                                                                                                                                        qw.lambda().eq(!StrUtil.isEmpty(query.getMaterialId()), CsmProductRecord::getMaterialId, query.getMaterialId());
                                                                                    qw.lambda().eq(!StrUtil.isEmpty(query.getStatus()), CsmProductRecord::getStatus, query.getStatus());
                                                                                                                                                            // 查询数据
        List<CsmProductRecord> list = super.list(qw);

        return convertList(list, CsmProductRecordDTO.class);
    }

    /**
     * 根据主键获取耗材生产记录信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
            CsmProductRecord item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted("1");

        this.updateById(item);
        }
}

